package com.ly.mpayment.persist.service.ele;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.ele.ElectronicAccount;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;


@Service
public class ElectronicAccountServiceImpl extends AbstractService<ElectronicAccount, Serializable> implements IElectronicAccountService {

	@Override
	public Page<ElectronicAccount> findPageElectronic(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select e from ElectronicAccount e where  1 = 1 #[ and e.accountName = :accountName]#  #[ and e.phoneNum = :phoneNum]# order by e.createTime desc ";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public ElectronicAccount findByid(String id) {
		return this.findUnique("select e from ElectronicAccount e where e.accId = ?", id);
	}

	@Override
	public boolean saveElectronicAccount(ElectronicAccount electronicAccount) {
		if(electronicAccount != null){
			this.saveOrUpdate(electronicAccount);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateElectronicAccount(ElectronicAccount electronicAccount) {
		if(electronicAccount != null){
			this.updateEntity(electronicAccount);
			return true;
		}
		return false;
	}

	@Override
	public ElectronicAccount findByNameAndPhone(String name) {
		List<ElectronicAccount> lists = this.find("select e from ElectronicAccount e where e.accountName = ? ", name);
		return lists == null || lists.isEmpty() ? null : lists.get(0);
	}

	/**
	 * 取出正在使用的第一个账号
	 */
	@Override
	public ElectronicAccount findByState(String accId) {
//		List<ElectronicAccount> lists = this.find("select e from ElectronicAccount e where e.accstate = 1 or e.accstate = 2");
//		return lists != null && !lists.isEmpty() ? lists.get(0) : null;
		return this.findUnique("select e from ElectronicAccount e where e.accId = ?", accId);
	}

	@Override
	public ElectronicAccount findByIdAndName(String id, String name) {
		return this.findUnique("select e from ElectronicAccount e where e.accId = ? and e.accountName = ?", id,name);
	}

	@Override
	public ElectronicAccount findByStateAndCity(String city) {
		List<ElectronicAccount> lists = this.find("select e from ElectronicAccount e where  e.accstate = 2 and e.city = '"+city+"'");
		return lists != null && !lists.isEmpty() ? lists.get(0) : null;
	}

	@Override
	public List<ElectronicAccount> findByState() {
		return this.find("select e from ElectronicAccount e where e.accstate = 1 or e.accstate = 2");
	}

}
